﻿/****************************************************************
*   作者：qq918
*   创建时间：2018/2/5 13:31:39
*   描述说明：
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Model
{
  /// <summary>
  /// UI跟随鼠标
  /// </summary>
  public class UIMouseFollow : MonoBehaviour
  {
    /// <summary>
    /// UI相机
    /// </summary>
    public Camera UICamera;
    public RectTransform Canvas;
    /// <summary>
    /// 偏移
    /// </summary>
    public Vector2 offset;

    /// <summary>
    /// 是否跟随
    /// </summary>
    public bool isFollow;

    private void LateUpdate()
    {
      if (isFollow)
      {
        Vector2 localPoint;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(Canvas, Input.mousePosition, UICamera, out localPoint))
        {
          localPoint += offset;
          transform.localPosition = localPoint;
        }
      }
    }
  }
}